<template>
  <div>
    <el-table
      :data="tableData"
      style="width: 100%;margin: 20px 20px;"
      row-key="id"
      border
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <el-table-column
        prop="name"
        label="名称"
        width="180">
      </el-table-column>
      <el-table-column
        prop="code"
        label="权限值">
      </el-table-column>
      <el-table-column
        prop="toCode"
        label="跳转权限值">
      </el-table-column>
      <el-table-column
        label="操作">
        <template #default="{row}">
          <el-button @click="isAddDialog(row)" :disabled="row.children.length <= 0" title="添加菜单" circle type="primary" size="small" icon="el-icon-plus"></el-button>
          <el-button @click="isUpdateDialog(row)" :disabled="row.name==='全部数据'" title="修改菜单" circle type="primary" size="small" icon="el-icon-edit-outline"></el-button>
          <el-button @click="deleteMenu(row)" :disabled="row.name==='全部数据'" title="删除" circle type="danger" size="small" icon="el-icon-delete"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加菜单/修改菜单 -->
    <el-dialog
      :title=" ruleForm.isFix === 0 ? ruleForm.level !== 4 ? '添加菜单' : '添加功能' : ruleForm.level !== 4 ? '修改菜单' : '修改功能'"
      :visible.sync="dialogVisible"
      width="40%">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="名称" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="功能权限值" prop="code">
          <el-input v-model="ruleForm.code"></el-input>
        </el-form-item>
        <el-form-item v-if="ruleForm.level === 4 " label="跳转路由权限值">
          <el-input v-model="ruleForm.toCode"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addMenu">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { reqAddMenuAsync, reqDeleteMenuAsync, reqGetMenuToAssignListAsync, reqUpdateMenuAsync } from '@/api/permission'
import { reqDeleteRoleItemAsync } from '@/api/role'

export default {
  name: 'Permission',
  data() {
    return {
      tableData: [], // 保存所有菜单权限
      dialogVisible: false, // 控制对话框的显示隐藏
      ruleForm: { // 收集表单数据
        name: '',
        code: '',
        level: -1,
        pid: '',
        toCode: '',
        type: -1,
        isFix: 0 // 控制是不是修改 0 不是 1 是
      },
      rules: { // 表单验证规则
        name: [
          { required: true, message: '请输入菜单名称', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入功能权限', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    // 初始获取所有的菜单
    this.getMenu()
  },
  methods: {
    // 获取初始菜单
    async getMenu() {
      try {
        const result = await reqGetMenuToAssignListAsync()
        this.tableData = result.data.children
      } catch (e) {
        this.$message.error('获取菜单失败')
      }
    },
    // 添加操作对话框的事件回调
    isAddDialog(row) {
      // 开始对话框
      this.dialogVisible = true
      // 收集添加菜单需要的数据
      this.ruleForm = {
        level: row.level + 1,
        pid: row.id,
        toCode: row.toCode,
        type: row.type,
        name: '',
        code: '',
        isFix: 0
      }
      // 清空上一次的表单数据
      this.ruleForm.name = ''
      this.ruleForm.code = ''
    },
    // 添加菜单
    async addMenu() {
      if (this.ruleForm.isFix === 0) { // 添加操作
        // 清除不必要的数据
        this.$delete(this.ruleForm, this.ruleForm.isFix)
        if (this.ruleForm.level === 4) {
          try {
            await reqAddMenuAsync(this.ruleForm)
            this.$message.success('添加功能成功')
            // 更新数据
            await this.getMenu()
          } catch (e) {
            this.$message.error('添加功能失败')
          }
        } else {
          try {
            await reqAddMenuAsync(this.ruleForm)
            this.$message.success('添加菜单成功')
            // 更新数据
            await this.getMenu()
          } catch (e) {
            this.$message.error('添加菜单失败')
          }
        }
      } else { // 修改操作
        // 清除不必要的数据
        this.$delete(this.ruleForm, 'isFix')
        try {
          await reqUpdateMenuAsync(this.ruleForm)
          this.$message.success('修改菜单成功')
          // 更新数据
          await this.getMenu()
        } catch (e) {
          this.$message.error('修改菜单失败')
        }
      }
      // 关闭对话框
      this.dialogVisible = false
      // 清空上一次添加或者修改的数据
      this.ruleForm = {
        name: '',
        code: '',
        level: -1,
        pid: '',
        toCode: '',
        type: -1,
        isFix: 0 // 控制是不是修改 0 不是 1 是
      }
    },
    // 修改操作对话框的事件回调
    isUpdateDialog(row) {
      // 开始对话框
      this.dialogVisible = true
      // 收集修改菜单需要的数据
      this.ruleForm = {
        ...row,
        isFix: 1 // 控制是不是修改
      }
    },
    // 删除操作
    async deleteMenu(row) {
      // try {
      //   await reqDeleteMenuAsync(row.id)
      //   this.$message.error(`删除${row.name}成功`)
      //   // 更新数据
      //   await this.getMenu()
      // } catch (e) {
      //   this.$message.error(`删除${row.name}失败`)
      // }
      this.$confirm(`确定删除${row.name}吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        try {
          await reqDeleteMenuAsync(row.id)
          // 更新数据
          await this.getMenu()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        } catch (e) {
          this.$message.error('删除失败')
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style scoped>

</style>
